package FileIOStream;

import java.io.File;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class Demo9 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的目录:");
        String rootDir = scanner.next();
        File rootFile = new File(rootDir);
        if(!rootFile.isDirectory()){
            System.out.println("非目录！");
            return ;
        }
        System.out.println("请输入关键字:");
        String keyWord = scanner.next();
        sanDir(rootFile,keyWord);
    }

    private static void sanDir(File rootFile, String keyWord) {
        File[] files = rootFile.listFiles();
        if(files == null){
            return ;
        }
        for(File file : files){
            if (file.isDirectory()) {
                sanDir(file,keyWord);
            }else {
                dealfile(file,keyWord);
            }
        }
    }

    private static void dealfile(File file, String keyWord) {
        if(file.getName().contains(keyWord)){
            Scanner scanner = new Scanner(System.in);
            System.out.println(file.getAbsolutePath()+"请输入是否删除:(n/y)");
            String ans = scanner.next();
            if(ans.equals("y")){
                file.delete();
                System.out.println("删除成功!");
            }
        }
    }
}







